/*
 * 
 * Raj Akula	
 * Inventory Look Up Android Application
 * CSC 594
 * 
 * DataHelper
 * Helps add and remove data from database..   
 * 
 */



package nku.csc594.project3;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class DataHelper {
	   private static final String TABLE_NAME = "items";
	 
	   private Context context;
	   private SQLiteDatabase db;
	 
	 
	   public DataHelper(Context context) {
	      this.context = context;
	      DatabaseHelper openHelper = new DatabaseHelper(this.context);
	      this.db = openHelper.getWritableDatabase();
	   }
	 
	   public Boolean insert(String category, String itemNumber, int qoh, float price, int pic){
			ContentValues cv = new ContentValues();
			cv.put("category", category);
			cv.put("itemnumber", itemNumber);
			cv.put("qoh", qoh);
			cv.put("price", price);
			cv.put("pict", pic);
		   this.db.insert(TABLE_NAME, null, cv);
		   return true;
	   }
	   
	   public Cursor getItem(String itemNumber){
		   Cursor mCursor = this.db.query(true, TABLE_NAME, new String[] {
					"category", "itemnumber", "qoh", "price", "pict" },
					"itemnumber" + "=\"" + itemNumber + "\"", null, null, null, null, null);
			if (mCursor != null) {
				mCursor.moveToFirst();	
			}
			return mCursor;
	   }
	 
	   public Boolean delete(String itemNumber){
		   return db.delete(TABLE_NAME, "itemnumber" + "=\"" + itemNumber + "\"", null) > 0;
	   }
	   
	   public void deleteAll() {
	      this.db.delete(TABLE_NAME, null, null);
	   }
	 
	   public List<String> selectAll() {
	      List<String> list = new ArrayList<String>();
	      Cursor cursor = this.db.query(TABLE_NAME, new String[] { "itemnumber" }, 
	        null, null, null, null, null);
	      if (cursor.moveToFirst()) {
	         do {
	            list.add(cursor.getString(0)); 
	         } while (cursor.moveToNext());
	      }
	      if (cursor != null && !cursor.isClosed()) {
	         cursor.close();
	      }
	      return list;
	   }
}
